package test.adddata;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import utils.SmallTool;
import utils.StringUtil;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: gcr
 * @Date: 2022-3-30 15:55
 */
public class 比对刚插入的部门doi是不是一样 {

    public static String fromTable = "xml_content_20200320_noexist";
    public static String fromTableParse = "pubmed_vip_extend_0328_33771437";
    public static String insertTable = "compare_doi";
    public static String insertTableSimple = "compare_doi2";

    @SneakyThrows
    public static void main(String[] args) {


        SmallTool.printMemory();

        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource ds = DSFactory.get("server_local");
        Db use = Db.use(ds);
        String fromSql = "select pmid from " + fromTable;
        List<Entity> query = use.query(fromSql);
        List<String> pmidStr = query.stream().map(o -> o.getStr("pmid")).collect(Collectors.toList());
        List<Integer> pmidInt = query.stream().map(o -> o.getInt("pmid")).collect(Collectors.toList());
        SmallTool.printMemory();
        List<Integer> noSame = new ArrayList<>(10000000);

        List<List<String>> split = ListUtil.split(pmidStr, 1000);
        for (List<String> strings : split) {
            String pmiddoi = "select pmid,doi_new,doi_boss from " + insertTable + " where pmid in (" + String.join(",", strings) + " )";
            List<Entity> queryPmidDoi = use.query(pmiddoi);
            SmallTool.printMemory();
            for (Entity entity : queryPmidDoi) {
                Integer pmidParam = entity.getInt("pmid");
                String doi_new = entity.getStr("doi_new");
                String doi_boss = entity.getStr("doi_boss");
                if (StringUtil.isNotNull(doi_new) && StringUtil.isNotNull(doi_boss) && !doi_new.equals(doi_boss)) {
                    noSame.add(pmidParam);
                }
            }
        }
        FileUtil.writeString(JSON.toJSONString(noSame), "/usr/local/gcr/nnnnsame.log", "UTF-8");

    }
}
